package com.mid;

/**
 * Created by Lxk on 2019/5/18.
 */




public class Solution209 {

    public static int minSubArrayLen(int s, int[] nums) {
        int sum = 0;
        int begin = -1;
        int beginSum = -1;
        boolean flag = true;
        for(int i = 0;i < nums.length; i++){
            sum += nums[i];
            if(sum>=s && flag){
                begin = i;
                beginSum = sum;
                flag = false;
            }
        }
        if(begin == -1){
            return 0;
        }else{
            int pos = 0;
            int min = Integer.MAX_VALUE;
            beginSum -= nums[begin];
            for(;begin < nums.length; begin++){
                beginSum += nums[begin];
                while(beginSum>=s){
                    beginSum -= nums[pos];
                    pos++;
                }
                beginSum += nums[--pos];
                if(begin-pos+1<min){
                    min = begin-pos+1;
                }
            }
            return min;
        }
    }




    public static void main(String[] args) {
        int[] arr = {5,1,3,5,10,7,4,9,2,8};
        System.out.println(minSubArrayLen(15,arr));
    }

}
